package ops.controller;

import java.util.ArrayList;
import java.util.Iterator;

import ops.WindowManager;
import ops.model.Course;
import ops.model.EntityAlreadyExistsException;
import ops.model.Subject;
import ops.model.manager.CourseManager;
import ops.model.manager.EntityController;
import ops.model.manager.SubjectManager;
import ops.view.CourseRegisterPanel;
import ops.view.CourseSubjectsDialog;

public class CourseController {
	
	private CourseManager courseManager;	
	
	private boolean adding;
	
	private int courseEditingSubjects;

	public CourseController() {
		courseManager = EntityController.getInstance().getCourseManager();
	}

	public void startAddProcess() {
		adding = true;
	}
	
	public void endAddProcess() {
		adding = false;
	}
	
	private void add(String name) {
		Integer code = courseManager.nextIntID();
		
		Course course = new Course();
		course.setCode(code);
		course.setName(name);
		course.setSubjects(new ArrayList<Integer>());
		
		try {
			courseManager.add(course);
			
			courseManager.save();
		} catch (EntityAlreadyExistsException e) {
			e.printStackTrace();
		}
	}
	
	private void edit(int code, String name) {
		Course course = courseManager.getObjectByKey(code);		
		course.setName(name);
		
		courseManager.save();
	}
	
	public void saveCourse(Integer code, String name) {
		if (adding) {
			add(name);
			
			adding = false;
		} else {
			edit(code, name);
		}
		
		updateTable();
	}
	
	public void removeCourse(int code) {
		courseManager.remove(code);
		courseManager.save();
		
		updateTable();
	}
	
	public void updateTable() {
		CourseRegisterPanel panel = getPanel();
		
		panel.clearTable();
		
		for (Course course : courseManager.getMap().values()) {
			panel.addRowToTable(course.getCode(), course.getName());
		}
	}
	
	public void updateSubjectsDialog(int code) {
		courseEditingSubjects = code; // must keep this somewhere.
		
		SubjectManager subjectManager = EntityController.getInstance().getSubjectManager();
		
		CourseSubjectsDialog dialog = getPanel().getCourseSubjectsDialog();
		dialog.clearAvailable();
		dialog.clearCurrent();
		
		Course course = courseManager.getObjectByKey(code);
		for (int subjectCode : course.getSubjects()) {
			Subject subject = subjectManager.getObjectByKey(subjectCode);
			dialog.addCurrent(subjectCode, subject.getName());
		}
		
		for (Subject subject : subjectManager.getMap().values()) {
			if (!course.getSubjects().contains(subject.getCode())) {
				dialog.addAvailable(subject.getCode(), subject.getName());
			}
		}				
	}
	
	public void addSubjectToCourse(int subjectCode) {
		Course course = courseManager.getObjectByKey(courseEditingSubjects);
		
		course.getSubjects().add(subjectCode);
		courseManager.save();
		
		updateSubjectsDialog(courseEditingSubjects);
	}
	
	public void removeSubjectCourse(int subjectCode) {
		Course course = courseManager.getObjectByKey(courseEditingSubjects);
		
		for (Iterator<Integer> iter = course.getSubjects().iterator(); iter.hasNext();) {
			int i = iter.next();
			if (i == subjectCode) {
				iter.remove();
			}
		}
		
		courseManager.save();
		
		updateSubjectsDialog(courseEditingSubjects);
	}
	
	private CourseRegisterPanel getPanel() {
		return (CourseRegisterPanel) WindowManager.getInstance().getPanel("register.courses");
	}
}
